package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cz.zslu.common.DateUtil;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.CompanyRecord;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.CompanyRecordService;
import com.cz.zslu.service.IBaseAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.util.Date;

/**
 * <p>
 * 公司档案 前端控制器
 * </p>
 *
 * @author Bruce
 * @since 2019-05-30
 */
@RestController
@RequestMapping("/sys/record")
@Api(description = "公司档案管理")
public class CompanyRecordController extends BaseController<CompanyRecordService, CompanyRecord> {

    @Autowired
    private CompanyRecordService companyRecordService;
    @Autowired
    private IBaseAreaService iBaseAreaService;


    @ApiOperation("获取详情")
    @IgnoreAuth
    @RequestMapping(value = "/findInfoByStoreId", method = RequestMethod.POST)
    public ReturnObject<CompanyRecord> findInfoByStoreId(@ApiParam("店铺id") @RequestParam Long storeId) {

        return new ReturnObject<>(0, "获取成功", companyRecordService.selectOne(new EntityWrapper<CompanyRecord>().eq("store_id", storeId)));
    }

    @ApiOperation("新增/编辑（新增时：id为空）")
    @RequestMapping(value = "/saveRecord", method = RequestMethod.POST)
    public ReturnObject<CompanyRecord> saveRecord(@ApiParam("企业名称") @RequestParam String companyName,
                                                  @ApiParam("联系人") @RequestParam String linkMan,
                                                  @ApiParam("电话") @RequestParam String phone,
                                                  @ApiParam("成立时间/成立日期") @RequestParam String establishmentDate,
                                                  @ApiParam("注册时间") @RequestParam String registrationDate,
                                                  @ApiParam("经营省id") Long businessProvinceId,
                                                  @ApiParam("经营市id") Long businessCityId,
                                                  @ApiParam("经营区id") Long businessDistrictId,
                                                  @ApiParam("经营地址") @RequestParam String businessAddress,
                                                  @ApiParam("经营范围") @RequestParam String businessScope,
                                                  @ApiParam("注册省id") Long registerProvinceId,
                                                  @ApiParam("注册市id") Long registerCityId,
                                                  @ApiParam("注册区id") Long registerDistrictId,
                                                  @ApiParam("注册地址") @RequestParam String registrationAddress,
                                                  @ApiParam("注册资本") @RequestParam String registeredCapital,
                                                  @ApiParam("注册号") @RequestParam String registeredNumber,
                                                  @ApiParam("法定代表人") @RequestParam String legalRepresentative,
                                                  @ApiParam("登记机关") @RequestParam String registrationAuthority,
                                                  @ApiParam("企业类型 0、有限责任公司 1、无限责任公司 2、个体经营 3、股份公司 4、两合公司") @RequestParam Integer enterpriseType,
                                                  @ApiParam("营业期限") @RequestParam String businessTerm,
                                                  @ApiParam("经营模式 0、生产厂家 1、经销批发 2、代理 3、其他") @RequestParam Integer businessModel,
                                                  @ApiParam("年检时间") @RequestParam String annualInspectionTime,
                                                  @ApiParam("商铺id") @RequestParam Long storeId,
                                                  @ApiParam("id") @RequestParam(defaultValue = "0") Long id) throws Exception{

        if (id != 0) {
            CompanyRecord info = companyRecordService.selectById(id);
            info.setCompanyName(companyName);
            info.setLinkMan(linkMan);
            info.setPhone(phone);
            info.setEstablishmentDate(establishmentDate);
            info.setRegistrationDate(registrationDate);
            String businessArea =
                    iBaseAreaService.selectById(businessProvinceId).getName() + iBaseAreaService.selectById(businessCityId).getName() + iBaseAreaService.selectById(businessDistrictId).getName();
            info.setBusinessArea(businessArea);
            info.setBusinessAddress(businessAddress);
            info.setBusinessScope(businessScope);
            String registerArea =
                    iBaseAreaService.selectById(registerProvinceId).getName() + iBaseAreaService.selectById(registerCityId).getName() + iBaseAreaService.selectById(registerDistrictId).getName();
            info.setRegisterArea(registerArea);
            info.setRegistrationAddress(registrationAddress);
            info.setRegisteredCapital(registeredCapital);
            info.setRegisteredNumber(registeredNumber);
            info.setLegalRepresentative(legalRepresentative);
            info.setRegistrationAuthority(registrationAuthority);
            info.setEnterpriseType(enterpriseType);
            info.setBusinessTerm(businessTerm);
            info.setBusinessModel(businessModel);
            info.setAnnualInspectionTime(annualInspectionTime);
            info.setStoreId(storeId);
            baseService.updateById(info);
        } else {

            CompanyRecord info = new CompanyRecord();
            info.setCompanyName(companyName);
            info.setLinkMan(linkMan);
            info.setPhone(phone);
            info.setEstablishmentDate(establishmentDate);
            info.setRegistrationDate(registrationDate);
            info.setBusinessAddress(businessAddress);
            info.setBusinessScope(businessScope);
            info.setRegistrationAddress(registrationAddress);
            info.setRegisteredCapital(registeredCapital);
            info.setRegisteredNumber(registeredNumber);
            info.setLegalRepresentative(legalRepresentative);
            info.setRegistrationAuthority(registrationAuthority);
            info.setEnterpriseType(enterpriseType);
            info.setBusinessTerm(businessTerm);
            info.setBusinessModel(businessModel);
            info.setAnnualInspectionTime(annualInspectionTime);
            info.setStoreId(storeId);
            baseService.insert(info);
        }
        return new ReturnObject<>(0, "新增成功", null);
    }
}

